package com.emotion.recognition.server.network;

import java.util.List;

import com.emotion.recognition.server.mlp.MLP;
import com.emotion.recognition.server.som.SOM;
import com.emotion.recognition.shared.Emotion;
import com.google.gwt.thirdparty.guava.common.collect.Lists;

public class NeuralNetwork {

	public static void runNeuralNetworks(List<List<Double>> training,
			List<List<Double>> targets, List<List<Double>> testing) {
		runMlp(training, targets, testing);
		runSom(training, targets, testing);
	}

	private static void runMlp(List<List<Double>> samples,
			List<List<Double>> targets, List<List<Double>> testing) {
		MLP net = MLP.builder().setInputLayer(400, 1.0d)
				.addHiddenLayer(4, 1.0d).setOutputLayer(4).setMaxEpochs(1000)
				.setDesiredMaxError(0.1).buildClassifier();

		net.train(samples, targets);

		for (List<Double> input : testing) {
			net.sim(input);
		}
	}

	private static void runSom(List<List<Double>> training,
			List<List<Double>> targets, List<List<Double>> testing) {
		SOM net = SOM.builder().setInputSize(4).setLatticeSize(8)
				.setLearningRate(0.25).setMaxEpochs(1000).setRadius(3)
				.buildClassifier();

		List<String> stringTargets = Lists.newArrayList();
		for (List<Double> target : targets) {
			stringTargets.add(Emotion.parseOneHotEncoded(target).toString());
		}

		net.classify(training, stringTargets);
	}
}
